package net.liteheaven.testfile;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by LiangYu on 2018/5/25.
 */
public class NyTime {

    private Calendar calendar;

    public static final String DATE = "yyyy-MM-dd";
    public static final String DATE_AND_TIME = "yyyy-MM-dd HH:mm";
    public static final String DISPLAY_DATE = "yyyy年MM月dd日";
    public static final String DISPLAY_DATE_AND_WEEKDAY = "yyyy年MM月dd日 EEEE";
    public static final String DISPLAY_DATE_AND_WEEKDAY_AND_TIME = "yyyy年MM月dd日 EEEE HH:mm";
    public static final String DISPLAY_WEEKDAY = "EEEE";
    public static final String DISPLAY_TIME = "HH:mm";
    public static final String DISPLAY_SIMPLE_DATE_AND_WEEKDAY = "MM月dd日 EEEE";

    public static final String DISPLAY_MMDDEE = "MM.dd (EE)";
    public static final String DISPLAY_DEBUG = "yyyy-MM-dd HH:mm:ss.SSS";

    public NyTime(String date){
        this(date, "00:00");
    }

    public NyTime(String date, String time){
        String timeStr = date + " " + time;
        Calendar calendar = date2Calendar(timeStr, DATE_AND_TIME);
        if (calendar != null){
            this.calendar = calendar;
        }
    }

    public NyTime(Calendar calendar){
        this.calendar = calendar;
    }

    public String getTimeString(String timeFormat){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeFormat);
        return simpleDateFormat.format(calendar.getTime());
    }

    public int getHour(){
        return calendar.get(Calendar.HOUR);
    }

    public String getTimeSegment(){
        String result = "";
        if (calendar != null){
            int hour24 = calendar.get(Calendar.HOUR_OF_DAY);
            if (hour24 >=0 && hour24 < 6){
                result = "凌晨";
            }else if (hour24 >=6 && hour24 < 8){
                result = "早上";
            }else if (hour24 >=8 && hour24 < 11){
                result = "上午";
            }else if (hour24 >=11 && hour24 < 13){
                result = "中午";
            }else if (hour24 >=13 && hour24 < 18){
                result = "下午";
            }else if (hour24 >=18){
                result = "晚上";
            }
        }
        return result;
    }

    public String get(){
        String str = DISPLAY_MMDDEE + " " + getTimeSegment() + " " + DISPLAY_TIME;
        return getTimeString(str);
    }

    private static Date dateStr2Date(String date, String format){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }

    private static Calendar date2Calendar(String date, String format){
        Date d = dateStr2Date(date, format);
        Calendar calendar = null;
        if (d != null){
            calendar = Calendar.getInstance(Locale.CHINA);
            calendar.setTime(d);
        }
        return calendar;
    }

    // -1=格式错误
    public static int getWeekDay(String date, String format){
        Calendar calendar = date2Calendar(date, format);
        if (calendar != null){
            int w = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
            if (w < 0)
                w = 0;
            return w;
        }
        return -1;
    }
}
